package com.ruoyi.flowable.listener.scrap;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.equ.domain.EquScrap;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.flowable.listener.asset.AssetListener_2.getFileList;

/**
 * 报废-2-4监听：技术评估小组
 */
@Slf4j
@Component("scrapListener_2_4")
public class ScrapListener_2_4 implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void notify(DelegateExecution execution) {
        try {
            //技术评估小组
            String flag5 = execution.getVariable("flag5", String.class);
            List<String> checkReport = getFileList(execution, "checkReport");
            HashMap<String, Object> techMap = new HashMap<>();
            techMap.put("是否有修复价值", flag5);
            techMap.put("评估报告", checkReport);

            EquScrap equScrap = execution.getVariable("equScrap", EquScrap.class);

            HashMap<String, Object> approvalSummaryMap = objectMapper.readValue(equScrap.getApprovalSummary(), new TypeReference<HashMap<String, Object>>() {});

            approvalSummaryMap.put("技术评估小组", techMap);
            String approvalSummary = objectMapper.writeValueAsString(approvalSummaryMap);

            //附件链接
            List<String> report = getFileList(execution, "checkReport");
            HashMap<String, Object> fileUrlsMap = objectMapper.readValue(equScrap.getFileUrls(), new TypeReference<HashMap<String, Object>>() {});
            fileUrlsMap.put("评估报告", report);
            String fileUrls = objectMapper.writeValueAsString(fileUrlsMap);

            equScrap.setApprovalSummary(approvalSummary);
            equScrap.setFileUrls(fileUrls);

            execution.setVariable("equScrap", equScrap);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
